package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.CloseStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminViewpointService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.result.ViewpointAdminList;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.result.ViewpointAdminListData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.vo.*;
import com.cloudbroker.bcs.platform.ltb.middle.entity.ViewpointMsg;
import com.cloudbroker.bcs.platform.ltb.middle.entity.ViewpointRecom;
import com.cloudbroker.bcs.platform.ltb.middle.entityview.ViewpointAdminView;
import com.cloudbroker.bcs.platform.ltb.middle.service.ViewpointMsgService;
import com.cloudbroker.bcs.platform.ltb.middle.service.ViewpointRecomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaosh19213 on 2016/7/22.
 */
@Service
public class AdminViewpointBizServiceImpl implements AdminViewpointService {

    @Resource
    private ViewpointRecomService viewpointRecomService;

    @Resource
    private ViewpointMsgService viewpointMsgService;

    /**
     * 观点推荐
     */
    public HSBCPSBackVO viewpointRecommend(AdminViewPointIdForm adminViewPointIdForm) {
        ViewpointMsg viewpointMsg =
                viewpointMsgService.get(adminViewPointIdForm.getViewpoint_id());
        if (viewpointMsg == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                    HSBCPSBackVO.class);
        }
        ViewpointRecom viewpointRecom = new ViewpointRecom();
        viewpointRecom.setViewpointmsg_id(Long.valueOf(adminViewPointIdForm.getViewpoint_id()));
        viewpointRecom.setCompany_id(adminViewPointIdForm.getCompany_id());
        List<ViewpointRecom> viewpointRecomlist = viewpointRecomService.query(viewpointRecom);
        if (viewpointRecomlist==null||viewpointRecomlist.size()==0){
            viewpointRecomService.save(viewpointRecom);
        }
        return VOUtil.genSuccessBackVo();
    }


    /**
     * 观点取消推荐
     */

    public HSBCPSBackVO viewpointRecommendCancel(AdminViewPointIdForm adminViewPointIdForm) {
        ViewpointRecom viewpointRecom = new ViewpointRecom();
        viewpointRecom.setViewpointmsg_id(Long.valueOf(adminViewPointIdForm.getViewpoint_id()));
        viewpointRecom.setCompany_id(adminViewPointIdForm.getCompany_id());
        viewpointRecomService.deleteByViewpoint(viewpointRecom);
        return VOUtil.genSuccessBackVo();
    }

    /**
     * 管理员获取观点列表
     */

    public ViewpointAdminList viewpointRecommendList(ViewpointAdminQueryForm viewpointAdminQueryForm) {
        List<ViewpointAdminView> viewpointAdminViews =
                viewpointRecomService.queryAdminViewpoint(viewpointAdminQueryForm);

        List<ViewpointAdminListData>  viewpointAdminListDataList = new ArrayList<>();
        for (ViewpointAdminView viewpointAdminView :viewpointAdminViews) {
            ViewpointAdminListData viewpointAdminListData = new ViewpointAdminListData();
            viewpointAdminListData.setViewpoint_id(viewpointAdminView.getViewpoint_id());
            viewpointAdminListData.setUpvote_num(viewpointAdminView.getUpvote_num());
            viewpointAdminListData.setShare_num(viewpointAdminView.getShare_num());
            viewpointAdminListData.setClose_status(viewpointAdminView.getClose_status());
            viewpointAdminListData.setViewpoint_content(viewpointAdminView.getViewpoint_content());
            viewpointAdminListData.setViewpoint_theme(viewpointAdminView.getViewpoint_theme());
            viewpointAdminListData.setUpdate_time(viewpointAdminView.getUpdate_time());
            viewpointAdminListData.setRecommend_flag(viewpointAdminView.getRecommend_flag());
            viewpointAdminListDataList.add(viewpointAdminListData);
        }

        ViewpointAdminList viewpointAdminList = new ViewpointAdminList();
        viewpointAdminList.setData_list(viewpointAdminListDataList);
        return viewpointAdminList;
    }

    /**
     * 公共管理观点列表
     */
    public ViewpointAdminList viewpointPublicManageList(ViewpointPublicManageQryForm viewpointPublicManageQryForm){
        ViewpointAdminQueryForm viewpointAdminQueryForm = new ViewpointAdminQueryForm();
        viewpointAdminQueryForm.setUser_id(String.valueOf(viewpointPublicManageQryForm.getUser_id()));
        viewpointAdminQueryForm.setCompany_id(viewpointPublicManageQryForm.getCompany_id());
        viewpointAdminQueryForm.setRequest_num(viewpointPublicManageQryForm.getRequest_num());
        viewpointAdminQueryForm.setPage_no(viewpointPublicManageQryForm.getPage_no());
        List<ViewpointAdminView> viewpointAdminViews =
                viewpointRecomService.queryAdminViewpoint(viewpointAdminQueryForm);

        List<ViewpointAdminListData>  viewpointAdminListDataList = new ArrayList<>();
        for (ViewpointAdminView view :viewpointAdminViews) {
            ViewpointAdminListData viewpointAdminListData = new ViewpointAdminListData();
            viewpointAdminListData.setViewpoint_id(view.getViewpoint_id());
            viewpointAdminListData.setUpvote_num(view.getUpvote_num());
            viewpointAdminListData.setShare_num(view.getShare_num());
            viewpointAdminListData.setClose_status(view.getClose_status());
            viewpointAdminListData.setViewpoint_content(view.getViewpoint_content());
            viewpointAdminListData.setViewpoint_theme(view.getViewpoint_theme());
            viewpointAdminListData.setUpdate_time(view.getUpdate_time());
            viewpointAdminListData.setRecommend_flag(view.getRecommend_flag());
            viewpointAdminListDataList.add(viewpointAdminListData);
        }

        ViewpointAdminList viewpointAdminList = new ViewpointAdminList();
        viewpointAdminList.setData_list(viewpointAdminListDataList);
        viewpointAdminList.setTotal_item_num(viewpointRecomService.countAdminViewpoint(viewpointAdminQueryForm));
        return VOUtil.setSuccessResult(viewpointAdminList);
    }
}
